package com.healthchart.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.healthchart.app.R;
import com.healthchart.app.bean.ValueBean;
import java.util.List;

public class MyAdapter extends BaseAdapter {
    private Context context;
    private int gw;
    private static int ROW_NUMBER = 3;
    private List<ValueBean> list;

    private String[] arrText = new String[]{
            "体重", "体脂率", "水分",
            "体质年龄", "内脏脂肪等级", "体重控制",
            "脂肪控制", "bml", "bmr"
    };
    private int[] arrImages = new int[]{
            R.mipmap.g1, R.mipmap.g2, R.mipmap.g3,
            R.mipmap.g4, R.mipmap.g5, R.mipmap.g6,
            R.mipmap.g7, R.mipmap.g8, R.mipmap.g9
    };

    public MyAdapter(Context context, int gw ,List<ValueBean> list) {
        this.context = context;
        this.gw = gw;
        this.list = list;
//        pictures = new ArrayList<>();
//        for (int i = 0; i < 9; i++) {
//            Picture pt = new Picture(arrText[i], arrImages[i]);
//            pictures.add(pt);
//        }

    }

    @Override
    public int getCount() {
            return 9;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_main, null);
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
            holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
            holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.img.setImageResource(arrImages[position]);
        switch (list.get(position).getState())
        {
            case 0:
                holder.tv1.setText("标准");
                holder.tv1.setBackgroundResource(R.mipmap.vi1);
                break;
            case 1:
                holder.tv1.setText("警戒");
                holder.tv1.setBackgroundResource(R.mipmap.vi2);
                break;
            case 2:
                holder.tv1.setText("偏高");
                holder.tv1.setBackgroundResource(R.mipmap.vi3);
                break;
                default:
                    holder.tv1.setText("标准");
                    holder.tv1.setBackgroundResource(R.mipmap.vi1);
                    break;
        }
        holder.tv2.setText(arrText[position]);
        holder.tv3.setText(list.get(position).getValue());


        //高度计算
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                gw / ROW_NUMBER,
                gw / ROW_NUMBER);//ViewGroup.LayoutParams.MATCH_PARENT
        convertView.setLayoutParams(param);
//        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                450);
//        convertView.setLayoutParams(param);
        return convertView;
    }

    class Holder {
        ImageView img;
        TextView tv1;
        TextView tv2;
        TextView tv3;
    }
}